package com.neusoft.yiyang.bed.controller;

import com.neusoft.yiyang.common.entity.bed.BedUsage;
import com.neusoft.yiyang.bed.service.BedUsageService;
import com.neusoft.yiyang.common.entity.bed.vto.BedCustomerUsage;
import com.neusoft.yiyang.common.entity.customer.Customer;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;
import java.util.Map;

@Tag(name = "床位使用记录接口", description = "管理床位使用记录的查询、更新、调换等操作")
@RestController
@RequestMapping("/bed-usage")
public class BedUsageController {

    @Autowired
    private BedUsageService bedUsageService;

    @Operation(summary = "根据客户名搜索客户", description = "通过客户姓名模糊查询客户列表（用于关联床位记录）")
    @GetMapping("/search")
    public List<Customer> searchCustomerByName(
            @Parameter(description = "客户姓名（支持模糊匹配）", required = true) @RequestParam String customerName) {
        return bedUsageService.searchCustomerByName(customerName);
    }

    @Operation(summary = "查询客户的床位使用记录", description = "获取指定客户的所有床位使用历史")
    @GetMapping("/{customerId}")
    public List<BedUsage> getBedUsageByCustomerId(
            @Parameter(description = "客户ID", required = true) @PathVariable Integer customerId) {
        return bedUsageService.getBedUsageByCustomerId(customerId);
    }

    @Operation(summary = "关闭床位使用记录", description = "为指定床位使用记录设置结束日期，标记为'已迁出'")
    @PutMapping("/close/{bedUsageId}")
    public void closeBedUsage(
            @Parameter(description = "床位使用记录ID", required = true) @PathVariable Integer bedUsageId,
            @Parameter(description = "结束日期（格式：yyyy-MM-dd）", required = true) @RequestParam Date endDate) {
        bedUsageService.closeBedUsage(bedUsageId, endDate);
    }

    @Operation(summary = "查询床位的历史使用记录", description = "获取指定床位的所有使用记录（历史客户）")
    @GetMapping("/history/{bedId}")
    public List<BedUsage> getBedUsageHistory(
            @Parameter(description = "床位ID", required = true) @PathVariable Integer bedId) {
        return bedUsageService.getBedUsageHistory(bedId);
    }

    @Operation(summary = "更新床位使用结束时间", description = "修改床位使用记录的结束日期")
    @GetMapping("updateBedEndTime")
    public void updateBedEndTime(
            @Parameter(description = "床位使用记录ID", required = true) @RequestParam Integer bedUsageId,
            @Parameter(description = "新的结束日期（格式：yyyy-MM-dd）", required = true)
            @RequestParam @DateTimeFormat(pattern = "yyyy-MM-dd") Date endDate) {
        bedUsageService.updateBedEndTime(bedUsageId, endDate);
    }

    @Operation(summary = "调换客户床位", description = "将客户从当前床位调换至新房间的新床位")
    @GetMapping("/swapBed")
    public void swapBed(
            @Parameter(description = "床位使用记录ID", required = true) @RequestParam Integer bedUsageId,
            @Parameter(description = "新房间号", required = true) @RequestParam String newRoomNumber,
            @Parameter(description = "新床位号", required = true) @RequestParam Integer newBedNumber
    ) {
        bedUsageService.swapBed(bedUsageId, newRoomNumber, newBedNumber);
    }

    @Operation(summary = "创建床位使用记录", description = "手动添加床位使用记录（通常用于初始化或特殊场景）")
    @PostMapping("/add")
    public void addBedUsage(
            @Parameter(description = "床位使用记录信息", required = true) @RequestBody BedUsage bedUsage) {
        bedUsageService.addBedUsage(bedUsage);
    }

    @Operation(summary = "查询客户当前的床位使用记录", description = "获取指定客户当前正在使用的床位记录")
    @GetMapping("/searchByCB")
    public BedUsage getBedusageByCid(
            @Parameter(description = "客户ID", required = true) @RequestParam Integer customerId) {
        return bedUsageService.getBedusageByCid(customerId);
    }

    @Operation(summary = "更新床位使用记录", description = "修改床位使用记录的详细信息（如入住日期、状态等）")
    @PutMapping("/update")
    public void updateBedUsage(
            @Parameter(description = "床位使用记录信息（含ID）", required = true) @RequestBody BedUsage bedUsage) {
        bedUsageService.updateBedUsage(bedUsage);
    }

    @Operation(summary = "批量调换床位", description = "同时调换多个客户的床位（参数为原记录与新记录的映射）")
    @PostMapping("/exchangeBeds")
    public void exchangeBeds(
            @Parameter(description = "床位调换映射（key=原记录，value=新记录）", required = true)
            @RequestBody Map<String, BedCustomerUsage> bedCustomerUsageMap) {
        bedUsageService.exchangeBeds(bedCustomerUsageMap);
    }
}